package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.User;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.Column;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 单据基类
 *
 * @author 袁首京<yuanshoujing@gmail.com>
 */
@MappedSuperclass
public abstract class Bill extends BaseEntity {

    protected static final long serialVersionUID = 3777696633404118235L;

    @Column(length = 30, nullable = false, updatable = false)
    protected String sn; // 单据编号

    @NotNull
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @Column(nullable = false)
    protected Date bizdate = new Date(); // 业务日期

    @NotNull
    @Column(nullable = false)
    protected Integer state = 0; // -1 标记删除 0 创建 1 生效

    @JSONField(serialize = false)
    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    protected User creator; // 创建人

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "create_time", nullable = false, insertable = false, updatable = false)
    protected Date createTime;

    @Transient
    protected String creatorId;
    @Transient
    protected String creatorName;

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public Date getBizdate() {
        return bizdate;
    }

    public void setBizdate(Date bizdate) {
        this.bizdate = bizdate;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCreatorId() {
        if (creatorId != null) {
            return creatorId;
        }

        if (creator != null) {
            creatorId = creator.getId();
        }

        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorName() {
        if (creatorName != null) {
            return creatorName;
        }

        if (creator != null) {
            creatorName = creator.getName();
        }

        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }
}
